﻿using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
using KPIS.DBM;
using KPIS.GERP.GIMS.DALFactory;
using KPIS.GERP.GIMS.IBLL;
using KPIS.GERP.GIMS.IDAL;
using KPIS.GERP.GIMS.MODEL;
using System.Web.UI;

namespace KPIS.GERP.GIMS.BLL
{
    public class DocumentBLL : IBLLDocument
    {
        private static readonly IDALDocument dal = DataAccess.CreateDocument();

        public DocumentBLL()
        {

        }

        public IList<DocumentInfo> GetDocument(int seq, string table_name, string field_name, string pk_field)
        {
            return dal.GetDocument(seq, table_name, field_name, pk_field);
        }

        public void InsertFileToDB(DocumentInfo info, string table_name, string fk_field, int seq)
        {
            try
            {
                dal.InsertFileToDB(info, table_name, fk_field, seq);
            }
            catch
            {
                throw;
            }
        }

        public void InsertFileToDB(IList<DocumentInfo> infoList, string table_name, string fk_field, int seq, IDBManager dbManagerTrans)
        {
            try
            {
                dal.InsertFileToDB(infoList, table_name, fk_field, seq, dbManagerTrans);
            }
            catch
            {
                throw;
            }
        }

        public void DeleteFileFromDB(IList<DocumentInfo> infoList, string table_name, string pk_field)
        {
            try
            {
                dal.DeleteFileFromDB(infoList, table_name, pk_field);
            }
            catch
            {
                throw;
            }
        }

        public void DownloadDocument(string table_name, string pk_field, string seq, System.Web.UI.Page p,string DocPath,string FolderName)
        {
            try
            {
                //string strFileName = "../Document/" +  table_name + seq;
                //string strFullFileName = p.Server.MapPath(strFileName);
                //string strFileType = dal.CreateDocumentFileFromDB(table_name, pk_field, seq, strFullFileName);
                //p.Response.ClearContent();
                //p.Response.ClearHeaders();
                //p.Response.ContentType = strFileType;

                //p.Response.WriteFile(strFullFileName);

                //p.Response.Flush();
                //p.Response.Close();
                //File.Delete(strFullFileName);
            }
            catch
            {
                throw;
            }
        }

        public IList<DocumentInfo> GetDocumentHistory(int PrSeq)
        {
            return dal.GetDocumentHistory(PrSeq);
        }

        public IList<DocumentInfo> GetDocumentPprHistory(int PoSeq)
        {
            return dal.GetDocumentPprHistory(PoSeq);
        }

        public IList<DocumentInfo> GetDocumentPrHistory(int PoSeq)
        {
            return dal.GetDocumentPrHistory(PoSeq);
        }
    }
}
  
